package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.Major;
import cn.qiyu5522.tk_ems.entity.vo.MajorVO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.MajorService;
import cn.qiyu5522.tk_ems.utils.RedisKey;
import cn.qiyu5522.tk_ems.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-04
 */
@Slf4j
@Api(tags = "专业管理")
@RestController
@RequestMapping("/admin/major")
public class MajorController {
    
    @Resource
    private MajorService majorService;
    @Resource
    private RedisUtil redisUtil;

    @WebLog(description="分页条件查询专业列表")
    @ApiOperation("专业查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true,dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated @RequestBody QueryParamDTO<MajorVO> queryParamDTO){
        return majorService.queryMajorByPageCondition(queryParamDTO);
    }

    @WebLog(description="专业新增--批量")
    @ApiOperation("专业新增")
    @ApiImplicitParam(name = "majorVOS", value = "专业新增", required = true,dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated @RequestBody List<MajorVO> majorVOS){
        return majorService.saveMore(majorVOS);
    }

    @WebLog(description="专业编辑")
    @ApiOperation("专业编辑")
    @ApiImplicitParam(name = "major", value = "专业实体类对象", required = true, defaultValue = "{}",dataTypeClass = Major.class)
    @PutMapping("/edit")
    public R edit(@Validated @RequestBody Major major){
        boolean b = majorService.updateById(major);
        // 更新成功，刷新redis 缓存
        if(b){
            if (b) {
                // 编辑成功，刷新Redis中的缓存
                // 查询全部的专业数据
                QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
                queryWrapper.select("id", "name","department_id");
                List<Major> majorList = majorService.list(queryWrapper);
                redisUtil.set(RedisKey.MAJOR_ID_AND_NAME, majorList, RedisKey.EXPIRE_ONE_DAY);
            }
        }
        return b ?R.ok().message("编辑成功"):R.error().message("编辑失败");
    }

    @WebLog(description="专业删除")
    @ApiOperation("专业删除")
    @ApiImplicitParam(name = "ids", value = "专业编号集合", required = true, defaultValue = "{}",dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids){
        return majorService.removeMore(ids);
    }


    @ApiOperation("查询全部专业编号与名称")
    @GetMapping("/queryForIdAndName")
    public R queryForIdAndName(){
        List<Major> forIdAndName = majorService.getForIdAndNameDepId();
        return R.ok().data("majorListForIdAndName", forIdAndName);
    }

    @ApiOperation("根据院系编号查询专业编号与名称")
    @ApiImplicitParam(name = "id",value = "院系编号",required = true,dataType = "Long")
    @GetMapping("/queryForIdAndNameByDepartmentId/{id}")
    public R queryForIdAndNameByDepartmentId(@NotNull(message = "根据院系编号查询专业编号与名称时,院系编号不能为空") @PathVariable Long id){

        return majorService.queryForIdAndNameByDepartmentId(id);
    }

    @ApiOperation("检测专业名称是否重复")
    @ApiImplicitParam(name="name",value = "专业名称", required = true, paramType = "path", dataType = "String")
    @GetMapping("/queryForCheckRepeatName/{name}")
    public R queryForCheckRepeatName(@NotBlank(message = "根据名称查询专业时,专业名称不能为空") @PathVariable String name){
        return majorService.queryByName(name);
    }


}
